<template>
  <el-container>
    <v-head :uname="username"></v-head>
    <el-aside width="220px">
      <v-sidebar></v-sidebar>
    </el-aside>
    <el-main>
      <router-view></router-view>
    </el-main>
  </el-container>
</template>

<script>
import vHead from "../components/header.vue";
import vSidebar from "../components/sidebar.vue";
import getList from "../utils/getList";
import queryList from "../utils/queryList";
import listHead from "../data/listHead";

export default {
  components: {
    vHead,
    vSidebar
  },
  data() {
    return {
      username: "",
      flag1: false,
      flag2: false
    };
  },
  created() {
    this.username = localStorage.username;
    this.sessionList();
    this.addressList();
  },
  mounted() {},
  methods: {
    sessionList() {
      getList("").then(res => {
        const listArray = res.data.data;
        listHead.forEach((sub, index) => {
          var arr = listArray.filter(list => {
            return list.groupId == sub.groupId;
          });
          sessionStorage.setItem(sub.name, JSON.stringify(arr));
        });
        this.flag1 = true;
        if (this.flag1 && this.flag2 && this.$route.path === "/")
          this.$router.push("/comManage/comList");
      });
    },
    addressList() {
      queryList("area", { parentId: "-1" })
        .then(res => {
          this.flag2 = true;
          if ((res.data.code = "200")) {
            sessionStorage.setItem(
              "addressProvince",
              JSON.stringify(res.data.data)
            );
            if (this.flag1 && this.flag2 && this.$route.path === "/")
              this.$router.push("/comManage/comList");
          } else {
            this.$message("系统繁忙，请稍后再试");
          }
        })
        .catch(err => {
          console.log(err);
          this.$message("系统繁忙，请稍后再试!");
        });
    }
  }
};
</script>
<style lang="less">
@import url("../assets/less/common.less");
</style>
